package info.niwota.icoe;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	public static final String ACTION_POWERUP_QUIT = "info.niwota.intent.ACTION_POWERUP_QUIT";

	private static final int RC_SETTINGS = 1;
	private static final String TAG = "MainActivity";
	private static final int RC_CALL = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onNewIntent " + intent);
		}
		super.onNewIntent(intent);

		setIntent(intent);
	}

	@Override
	protected void onPause() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause");
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume");
		}
		super.onResume();

		initUi();
	}

	@Override
	protected void onDestroy() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onDestroy");
		}
		super.onDestroy();
	}

	@Override
	protected void onStart() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onStart");
		}
		super.onStart();
	}

	@Override
	protected void onStop() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onStop");
		}
		super.onStop();
	}

	private void initUi() {
		final Context ctx = this;

		try {
			Profile p = Preferences.getProfile(ctx);
			setText(R.id.profile_name, p.name);
			setText(R.id.profile_bloodtype, p.bloodType, R.string.fmt_bloodtype);
			setText(R.id.profile_note, p.note);

			final int max = 5;
			final int[] ids = new int[] { R.id.contact1, R.id.contact2,
					R.id.contact3, R.id.contact4, R.id.contact5 };

			for (int i = 0; i < max; i++) {
				final String key = String.valueOf(i);
				Contact c = Preferences.getContact(ctx, key);
				View v = findViewById(ids[i]);
				if (!c.isEmpty()) {
					setText(v, R.id.contact_name, c.name);
					setText(v, R.id.contact_relation, c.relation);
					setText(v, R.id.contact_phone, c.phone);
					setText(v, R.id.contact_note, c.note);
					v.setVisibility(View.VISIBLE);

					v.setOnClickListener(this);

				} else {
					v.setVisibility(View.GONE);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setText(int id, String text, int fmt) {
		TextView f = (TextView) findViewById(id);
		if (!TextUtils.isEmpty(text)) {
			String t = getResources().getString(fmt, text);
			f.setText(t);
		} else {
			f.setText(null);
		}
	}

	private void setText(View v, int id, String text) {
		TextView f = (TextView) v.findViewById(id);
		f.setText(text);
	}

	private void setText(int id, String text) {
		TextView f = (TextView) findViewById(id);
		f.setText(text);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onActivityResult " + data);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onOptionsItemSelected ");
		}
		if (isGuarded()) {
			return false;
		}
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_options, menu);
		//
		return true;
	}

	protected boolean isGuarded() {
		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onOptionsItemSelected ");
		}
		if (isGuarded()) {
			return true;
		}
		switch (item.getItemId()) {
		case R.id.start_help_opt_menu_id:
			doHelp();
			return true;
		case R.id.start_settings_opt_menu_id:
			doSettings();
			return true;
		case R.id.start_quit_opt_menu_id:
			doQuit();
			return true;
		default:
		}
		return false;
	}

	private void doQuit() {
		finish();
	}

	private void doHelp() {
		WebViewActivity.open(this);
	}

	private void doSettings() {
		try {
			Intent i = new Intent(this, SettingsActivity.class);
			startActivityForResult(i, RC_SETTINGS);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void doCall(String phone) {

		try {
			Activity ctx = this;

			if (!TextUtils.isEmpty(phone)) {
				Intent i = new Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
						phone, null));
				ctx.startActivityForResult(i, RC_CALL);
			}
		} catch (Exception e) {
			if (Debug.DEBUG) {
				e.printStackTrace();
			}
			showError(e);
		}
	}

	private void showError(Exception e) {
		Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onKeyDown " + isGuarded() + " key: " + keyCode);
		}
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return isGuarded();
	}
	
	public void onClick(View v) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onClick ");
		}
		//
		String phone = ((TextView) v.findViewById(R.id.contact_phone))
				.getText().toString();
		if (Debug.DEBUG) {
			Log.d(TAG, "onClick: " + phone);
		}
		//
		doCall(phone);
	}
}